const db = require('../config/db')

class Banner {
    // 添加轮播图列表 // db_lqybanner
    add(banner_title, banner_link, banner_picUrl, banner_create_time) {
        return new Promise((resolve, reject) => {
            // 查询db_message这个表格的所有字段
            let sql = `insert into db_lqybanner(banner_title, banner_link, banner_picUrl, banner_create_time) values('${banner_title}','${banner_link}','${banner_picUrl}','${banner_create_time}')`;
            db.query(`select * from db_lqybanner where banner_title='${banner_title}'`)
                .then(
                    rows => {
                        if (rows.length === 0) {
                            // 添加
                            db.query(sql)
                                .then(
                                    content => {
                                        resolve({
                                            code: 200,
                                            msg: "发布轮播图成功！",
                                            content
                                        })
                                    }
                                )
                                .catch(
                                    error => {
                                        resolve({
                                            code: 500,
                                            msg: "发布轮播图失败！",
                                            error,
                                        })
                                    }
                                )
                        } else {
                            resolve({
                                code: 1,
                                msg: "该标题对应的轮播图已经存在，请忽重复添加！",
                                rows,
                            })
                        }
                    }
                )
                .catch(
                    err => {
                        reject({
                            code: 404,
                            msg: "服务器异常！",
                            err,
                        })
                    }
                )
        })
    }


    // 删除轮播图列表
    remove(id) {
        return new Promise((resolve, reject) => {
            // 根据ID查询用户信息是否存在
            db.query(`SELECT * FROM db_lqybanner WHERE id = '${id}'`)
                .then(  // 请求成功
                    rows => {
                        if (rows && rows.length === 1) {
                            // 账号存在，执行删除账号的操作
                            let sql = `delete from db_lqybanner where id = '${id}'`
                            db.query(sql)
                                .then(
                                    content => {
                                        resolve({
                                            code: 200,
                                            msg: "删除轮播图列表信息成功！",
                                            content,
                                        });
                                    })
                                .catch(
                                    err => {
                                        reject({
                                            code: -1,
                                            msg: "删除轮播图列表信息失败！",
                                            err,
                                        });
                                    });
                        } else {
                            // 账号不存在
                            resolve({
                                code: 404,
                                msg: `不存在${user_name}该轮播图列表！`,
                            });
                        }
                    })
                .catch( // 抓取异常
                    err => {
                        reject({
                            code: -1,
                            msg: "查询数据库失败！",
                            err,
                        });
                    });
        })

    }


    // 统计数量
    total() {
        return new Promise((resolve, reject) => {
            let sql = 'select count(*) as total from db_lqybanner';
            db.query(sql)
                .then(
                    rows => {
                        resolve({
                            code: 200,
                            msg: "统计轮播图列表信息成功！",
                            rows,
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: "统计轮播图列表信息失败！",
                            err,
                        })
                    }
                )
        });
    }


    // 获取轮播图列表信息
    async lists(page, pageSize) {
        // 统计数量
        let total;
        await this.total()
            .then(
                content => {
                    total = content.rows[0].total
                }
            )
            .catch(
                err => {
                    console.log("我没错！我想飞~~~", err);
                }
            )

        return new Promise((resolve, reject) => {
            let sql = `select * from db_lqybanner limit ${page} , ${pageSize}`;
            db.query(sql)
                .then(
                    rows => {
                        resolve({
                            code: 200,
                            total,
                            msg: "列表分页查询轮播图列表信息成功！",
                            rows,
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: "列表分页查询轮播图列表信息失败！",
                            err,
                        })
                    }
                )
        });
    }

}

module.exports = new Banner();
